######################### MAP GEOLOCATED USERS ############################
#############################################################################
#Name of code file: egypt_geo.R
#Purpose: Map Geolocated Twitter Users 
#Data In: egypt_geo.csv
#Data Out: Figure 2
######################################################################

#Load Packages
library(readr)
library(dplyr)
require(viridis)
library(geometry)
library(sf)
library(maps)     
library(mapdata)
library(mapview)


#Set Working Directory
setwd("~/Dropbox/egypt_tolerance_wp_replication/")

#Read in location data
data<-read_csv("data/egypt_geo.csv")

#Count Prevalence of Locations 
data<-na.omit(data)
data$count<-1

locations_count<-data %>%
  group_by(lat, lng) %>%
  summarise(freq=sum(count))

#Map Egypt
locations_sf <- st_as_sf(locations_count, coords = c("lng", "lat"))

world = spData::world

mapviewOptions(basemaps = c("Esri.WorldShadedRelief", "OpenStreetMap.DE", "CartoDB.Positron", "CartoDB.VoyagerNoLabels", "CartoDB.Voyager"),
               raster.palette = grey.colors,
               vector.palette = colorRampPalette(c("black", "black", "black")),
               na.color = "white",
               layers.control.pos = "topright")

mapview(world)+
    mapview(locations_sf, cex="freq", legend=FALSE, color="black")

#To create Figure 2 select CartoDB.Voyager in Viewer and remove "world" layer 
  